import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hc_bot_kit/service/service.dart';
import 'package:hc_bot_kit/state.dart';
import 'package:lottie/lottie.dart';

class ProgressView extends StatefulWidget {
  const ProgressView({super.key});

  @override
  State<ProgressView> createState() => _ProgressViewState();
}

class _ProgressViewState extends State<ProgressView> {
  String title = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Get.find<RobotHardwareService>().onNavigationStateChanged = onGuide;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        LottieBuilder.asset("assets/images/robot_guide.json"),
        Text(
          "正在导航到目的地:${title}",
          style: TextStyle(fontSize: 18, color: Colors.white),
        ),
      ],
    );
  }

  void onGuide(GuideState guideState) {
    switch (guideState) {
      case GuideState.START:
        {
          final dst = Get.find<RobotHardwareService>().lastDestination;
          setState(() {
            title = dst;
          });
        }
        break;
      case GuideState.REACHED:
        {
          final service = Get.find<RobotHardwareService>();
          final dst = Get.find<RobotHardwareService>().lastDestination;
          if (dst == service.originPoi) {
            Get.back();
          } else {
            service.guide(service.originPoi);
          }
        }
        break;
      default:
        break;
    }
  }
}
